﻿using ASmile;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace ASmile.Web
{
    public class GlobalExceptionsFilter : Attribute, IExceptionFilter
    {
        private readonly IWebHostEnvironment _Environment;

        public GlobalExceptionsFilter(IWebHostEnvironment environment)
        {
            this._Environment = environment;
        }

        public void OnException(ExceptionContext context)
        {
            Exception exception = context.Exception;

            LogHelper.LogObj.Error("服务器发生异常-GlobalExceptions", exception);

            ContentResult result = new ContentResult
            {
                Content = "服务器发生异常，请联系管理员。",
                StatusCode = 500,
                ContentType = "text/json;charset=utf-8;"
            };

            context.Result = result;
            context.ExceptionHandled = true;
        }
    }
}